package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.MyCollectNote;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/5/5.
 */

public class MyCollectAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<MyCollectNote.ListBean> list;
    private LayoutInflater layoutInflater;
    private boolean bottom;
    private int bottom_count,tag;//是否显示底部，底部显示文字

    public MyCollectAdapter(Context context, List<MyCollectNote.ListBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<MyCollectNote.ListBean> list,int bottom_count,int tag){
        this.list = list;
        this.bottom_count=bottom_count;
        this.tag=tag;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view_weizhi=layoutInflater.inflate(R.layout.manager_item_weizhi,viewGroup,false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view,viewGroup,false);
        AutoUtils.autoSize(view_weizhi);
        AutoUtils.autoSize(view_bottom);
        if(i==ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else{
            return new WeiZhiViewHolder(view_weizhi);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof WeiZhiViewHolder ){
            ((WeiZhiViewHolder) holder).position=position;
            ((WeiZhiViewHolder) holder).weizhi_title.setText(list.get(position).getOrder().getTitle());
            ((WeiZhiViewHolder) holder).weizhi_num.setText(list.get(position).getOrder().getDealCount()+"");
            ((WeiZhiViewHolder) holder).weizhi_time.setText(list.get(position).getOrder().getCreateTime());
            ((WeiZhiViewHolder) holder).weizhi_name.setText("发布者 : "+list.get(position).getOrder().getRealName());
            switch (list.get(position).getOrder().getPriceMethod()){
                case 0:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/次");//服务单价
                    break;
                case 1:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/时");//服务单价
                    break;
                case 2:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/天");//服务单价
                    break;
                case 3:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/周");//服务单价
                    break;
                case 4:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/月");//服务单价
                    break;
                case 5:
                    ((WeiZhiViewHolder) holder).weizhi_price.setText(list.get(position).getOrder().getPrice()+"/年");//服务单价
                    break;
            }
        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&tag==0){
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&tag==1){
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&tag==2){
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(list!=null&&position==list.size()){
            return ManagerItem.Bottom;
        }else{
            return super.getItemViewType(position);
        }

    }

    @Override
    public int getItemCount() {
        return list==null?0+bottom_count:list.size()+bottom_count;
    }

    class WeiZhiViewHolder extends RecyclerView.ViewHolder{
        int position;
        TextView weizhi_title,weizhi_classify,weizhi_num,weizhi_time,weizhi_name,weizhi_price;
        RelativeLayout weizhi_itme_view;
        public WeiZhiViewHolder(View itemView) {
            super(itemView);
            weizhi_itme_view= (RelativeLayout) itemView.findViewById(R.id.weizhi_itme_view);
            weizhi_title= (TextView) itemView.findViewById(R.id.weizhi_title);
            weizhi_classify= (TextView) itemView.findViewById(R.id.weizhi_classify);
            weizhi_num= (TextView) itemView.findViewById(R.id.weizhi_num);
            weizhi_time= (TextView) itemView.findViewById(R.id.weizhi_time);
            weizhi_name= (TextView) itemView.findViewById(R.id.weizhi_name);
            weizhi_price= (TextView) itemView.findViewById(R.id.weizhi_price);
            weizhi_itme_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson=new Gson();
                    String json=gson.toJson(list.get(position));
                    MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.CollectWeiZhi+""});
                }
            });
            weizhi_itme_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Log.i("MyCollectAdapter","删除");
                    new AlertDialog.Builder(context).setMessage("是否删除").setNegativeButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            MyOkHttpUtils.getInstance(context).doDetele(Constants.ShouCang+"/"+list.get(position).getOrder().getId())
                                    .excute(new MyOkHttpUtils.OKCallBack() {
                                        @Override
                                        public void onFailure(String message) {

                                        }

                                        @Override
                                        public void onResponse(String message) {
                                            Log.i("MyCollectAdapter","message==delete=="+message);
                                            if(message!=null){
                                                try {
                                                    JSONObject jsonObject=new JSONObject(message);
                                                    boolean result=jsonObject.getBoolean("result");
                                                    if(result){
                                                        list.remove(position);
                                                        UpData(list,0,0);
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    });
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).show();
                    return false;
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
